/**
 * Enhance vids
 */
var enhance = {
	
	isChanged : [false, false],
	
	toggle : function(key) {
		debug.print("enhance.show [key: "+key+"]");
		if (!player[key].video) {
			flash.flash('No video', 'loaded');
			return false;
		};
		if ($("#vid" + key + "-actions > div.enhance").css("display") == "block")  {
			if (enhance.isChanged[key] === true) {
				if (confirm("Discard changes?")) {
					this.hide(key);
				}
			} else {
				this.hide(key);
			}
		} else {
			this.show(key);
			this.init(key);
		}
	},
	
	show : function(key) {
		actions.show(key, "enhance");
	},
	
	hide : function(key) {
		actions.hide(key);
	},
	
	init : function(key) {
		debug.print("enhance.init [key: "+key+"]");
		$("#vid" + key + "-actions > div.enhance .enhance-save").removeClass("changed");
		$("#vid" + key + "-actions > div.enhance .enhance-artist").attr("value", player[key].video.artist);
		$("#vid" + key + "-actions > div.enhance .enhance-title").attr("value", player[key].video.title);
		$("#vid" + key + "-actions > div.enhance .enhance-flags a").removeClass("on");
		enhance.isChanged[key] = false;
	},
	
	change : function(obj) {
		debug.print("enhance.change [obj: "+obj+"]");
		var key = parseInt($(obj).parent().parent().parent().attr("id").substr(3,1));
		$("#vid" + key + "-actions > div.enhance .enhance-save").addClass("changed");
		enhance.isChanged[key] = true;
	},
	
	toggleTag : function(obj) {
		debug.print("enhance.init [obj: "+obj+"]");
		$(obj).toggleClass("on");
	},
	
	isOn : function(key, tag) {
		debug.print("enhance.isOn [key: "+key+"]");
		return $("#vid" + key + "-actions > div.enhance a.enhance-" + tag).hasClass("on");
	},
	
	save : function(key) {
		debug.print("enhance.save [key: "+key+"]");
		if (enhance.isChanged[key] === false) {
			flash.flash("No changes","made");
			return false;
		};
		$.post(
			"/enhance/submit/json",
			{
				video_id: player[key].video.id,
				artist: $("#vid" + key + "-actions > div.enhance .enhance-artist").attr("value"),
				title: $("#vid" + key + "-actions > div.enhance .enhance-title").attr("value"),
				tag_live: enhance.isOn(key, "live"),
				tag_nomusic: enhance.isOn(key, "nomusic"),
				tag_bad: enhance.isOn(key, "bad")
			},
			function(data) {
				if (data.result === true) {
					flash.flash('Edit','saved');
					enhance.isChanged[key] = false;
					enhance.toggle(key);
				} else {
					flash.flash('Edit','not saved (sorry, something went wrong)');
				}								
			},
			"json"
		);
	}
	
}
